home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / iteratordemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  2.9 KB  |  107 lines

  1. {
  2. GPC demo program. How to use procedural parameters and pass global
  3. and local procedures to them without the need for any dirty tricks
  4. (e.g. assembler code) that other compilers require. This demo
  5. program uses procedural parameters to implement a list iterator.
  6.  
  7. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  8.  
  9. Author: Frank Heckenbach <frank@pascal.gnu.de>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, version 2.
  14.  
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with this program; see the file COPYING. If not, write to
  22. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. Boston, MA 02111-1307, USA.
  24.  
  25. As a special exception, if you incorporate even large parts of the
  26. code of this demo program into another program with substantially
  27. different functionality, this does not cause the other program to
  28. be covered by the GNU General Public License. This exception does
  29. not however invalidate any other reasons why it might be covered
  30. by the GNU General Public License.
  31. }
  32.  
  33. program IteratorDemo;
  34.  
  35. type
  36.   PList = ^TList;
  37.   TList = record
  38.     Next : PList;
  39.     Val : Integer
  40.   end;
  41.  
  42. { Define an iterator using a procedural parameter. The following is the
  43.   Standard Pascal syntax. GPC also supports the BP syntax (defining a
  44.   procedural type in advance), but that would be longer in this case. }
  45. procedure ForEach (p : PList; procedure Proc (var Element : TList));
  46. begin
  47.   while p <> nil do
  48.     begin
  49.       Proc (p^);
  50.       p := p^.Next
  51.     end
  52. end;
  53.  
  54. procedure ListDemo;
  55. var
  56.   List : PList;
  57.   a : Integer;
  58.  
  59.   procedure ReadList;
  60.   var
  61.     pp : ^PList;
  62.     i : Integer;
  63.   begin
  64.     pp := @List;
  65.     Writeln ('Enter some numbers. Enter an empty line when finished.');
  66.     repeat
  67.       Read (i);
  68.       New (pp^);
  69.       pp^^.Val := i;
  70.       pp := @pp^^.Next;
  71.       if EOLn then Readln
  72.     until EOLn;
  73.     pp^ := nil
  74.   end;
  75.  
  76.   procedure WriteElement (var Element : TList);
  77.   begin
  78.     Write (Element.Val : 8)
  79.   end;
  80.  
  81.   procedure AddToElement (var Element : TList);
  82.   begin
  83.     Inc (Element.Val, a) { This procedure can access the variable a without
  84.                            problems, even though it is called by ForEach,
  85.                            and ForEach doesn't know about a. }
  86.   end;
  87.  
  88. begin
  89.   ReadList;
  90.   Writeln;
  91.   Writeln ('The values are:');
  92.   ForEach (List, WriteElement);
  93.   Writeln;
  94.   Writeln;
  95.   Write ('Enter a number to add to all elements in the list: ');
  96.   Readln (a);
  97.   ForEach (List, AddToElement);
  98.   Writeln;
  99.   Writeln ('The values are now:');
  100.   ForEach (List, WriteElement);
  101.   Writeln
  102. end;
  103.  
  104. begin
  105.   ListDemo
  106. end.
  107.